<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function test() {
            var n = 1;
            function add() {
                n++;
                console.log(n);
            }
            return {
                n: n,
                add: add
            }
        }

        var result = test();
        var result2 = test();
        
        result.add();
        result.add();
        console.log(result.n);
        result2.add();
    </script>
</body>

</html>